﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using CandidateManager.App_Code.Helpers;

namespace CandidateManager.Controllers
{
    public class BaseController : Controller
    {
        /// <summary>
        /// Overriden Initialize for Localization with validation
        /// </summary>
        /// <param name="ctx">The CTX.</param>
        protected override void Initialize(System.Web.Routing.RequestContext ctx)
        {
            HttpCookie cookieCulture;
            if (ctx.HttpContext.Request.Cookies["lang"] != null)
            {
                cookieCulture = LocalizationHelper.GetValidLanguageCookie(ctx.HttpContext.Request.Cookies["lang"]);
            }
            else
            {
                cookieCulture = LocalizationHelper.GetDefaultLanguageCookie();
                ctx.HttpContext.Response.Cookies.Add(cookieCulture);
            }
            CultureInfo ci = new CultureInfo(cookieCulture.Value);
            Thread.CurrentThread.CurrentUICulture = ci;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);

            base.Initialize(ctx);
        }
    }
}
